package com.msite.web
import com.msite.framework.JsonResult
import com.msite.model.SysUser
import com.msite.service.SysMenuService
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.ui.ModelMap
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody

import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
/**
 * Created by tpeng on 2015/10/16.
 */
@Controller
class IndexController extends BaseController{

    @Resource
    SysMenuService sysMenuService;

    /*@RequestMapping(value = "/login_")
    public String login_(ModelMap model, HttpServletRequest request, HttpSession session){
        def exp = session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION")
        def errorMsg = exp ? new String(((Exception)exp).message) : ""
        model.put("errorMsg", errorMsg);
        return "/login";
    }*/

    @RequestMapping(value = "/timeout_")
    public String timeout_(ModelMap model, HttpServletRequest request){
        return "/timeout";
    }

    @RequestMapping(value = "/login")
    @ResponseBody
    public JsonResult login(Model model, SysUser user){
        /*try {
            Subject subject = SecurityUtils.getSubject();
            subject.login(new UsernamePasswordToken(user.username, user.password));
            if (subject.authenticated) {
                return new JsonResult<>(code: JsonResult.CODE_OK, msg: "登录成功!");
            }else {
                return new JsonResult<>(code: JsonResult.CODE_FAIL, msg: "用户名或密码错误!");
            } 
        } catch (Exception ex) {
            return new JsonResult(code: JsonResult.CODE_FAIL, msg: "用户名或密码错误!")
        }*/
        return new JsonResult(code: JsonResult.CODE_FAIL, msg: "用户名或密码错误!")
    }

    @RequestMapping(value = "/index_")
    public String index_(ModelMap model){
        return "/index";
    }

    @RequestMapping(value = "/welcome")
    public String welcome(ModelMap model){
        return "/welcome";
    }

    /**
     * 首页导航栏菜单数据
     * @param model
     * @return
     */
    @RequestMapping(value = "/menu/tree/data")
    @ResponseBody
    public JsonResult menu_tree_data(ModelMap model){
        def rdata = sysMenuService.selectMenuMapByUserId(1L)
        new JsonResult(JsonResult.CODE_OK, "", rdata)
    }
}
